home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / Demos / AirHockey / modD3D.bas < prev    next >
Encoding:
BASIC Source File  |  2001-10-08  |  10.3 KB  |  265 lines

  1. Attribute VB_Name = "modD3D"
  2. Option Explicit
  3.  
  4. Public dx As New DirectX8
  5.  
  6. Public gfDrawRoomText As Boolean
  7. Public glScreenWidth As Long
  8. Public glScreenHeight As Long
  9.  
  10. 'Extra misc vars
  11. Public gfWireFrame As Boolean
  12.  
  13. Public Sub Render()
  14.  
  15.     Dim lCount As Long
  16.     
  17.     On Error Resume Next
  18.     If g_dev Is Nothing Then Exit Sub
  19.  
  20.     ' Clear the backbuffer to a black color
  21.     g_dev.Clear 0, ByVal 0, D3DCLEAR_TARGET Or D3DCLEAR_ZBUFFER, &H0&, 1#, 0
  22.      
  23.     ' Setup the world, view, and projection matrices
  24.     SetupMatrices
  25.  
  26.     ' Begin the scene
  27.     g_dev.BeginScene
  28.     
  29.     'Draw everything in either a solid fillmode, or wireframe
  30.     If gfWireFrame Then
  31.         g_dev.SetRenderState D3DRS_FILLMODE, D3DFILL_WIREFRAME
  32.     Else
  33.         g_dev.SetRenderState D3DRS_FILLMODE, D3DFILL_SOLID
  34.     End If
  35.     
  36.     'Render the room
  37.     goRoom.Render g_dev
  38.     
  39.     'Render the table
  40.     goTable.Render g_dev
  41.     
  42.     'Now Paddle (0)
  43.     goPaddle(0).Render g_dev
  44.     
  45.     'Now Paddle (1)
  46.     goPaddle(1).Render g_dev
  47.     
  48.     'And finally the puck
  49.     goPuck.Render g_dev
  50.     'Now lets draw whatever text we need
  51.     
  52.     'We can draw text (don't draw text if we're currently fading)
  53.     If Not goFade.AmFading Then
  54.         goTextLittle.BeginText
  55.         If gfGameCanBeStarted Then
  56.             'If the game can be started, then draw the scores at the top of the screen
  57.             If gfMultiplayer Then
  58.                 If glMyPaddleID = 0 Then
  59.                     goTextLittle.DrawText gsUserName & ":" & glPlayerScore(0), 10, 5, &HFFFFFF00
  60.                 Else
  61.                     goTextLittle.DrawText "Opponent:" & glPlayerScore(0), 10, 5, &HFFFFFFFF
  62.                 End If
  63.                 If glMyPaddleID = 1 Then
  64.                     goTextLittle.DrawText gsUserName & ":" & glPlayerScore(1), glScreenWidth - 75, 5, &HFFFFFF00
  65.                 Else
  66.                     goTextLittle.DrawText "Opponent:" & glPlayerScore(1), glScreenWidth - 75, 5, &HFFFFFFFF
  67.                 End If
  68.             Else
  69.                 goTextLittle.DrawText "Player:" & glPlayerScore(0), 10, 5, &HFFFFFF00
  70.                 goTextLittle.DrawText "Computer:" & glPlayerScore(1), glScreenWidth - 75, 5, &HFFFFFFFF
  71.             End If
  72.         Else
  73.             'The game can't be started yet (only in multiplayer)  Let the host know
  74.             goTextLittle.DrawText "Waiting for the game to be started...", (glScreenWidth / 2) - 50, 5, &HFFFFFFFF
  75.         End If
  76.         'Here is a little helper text letting the user know to press Space
  77.         'to launch the puck (will show up after 3 seconds, and stay on for 10 seconds)
  78.         If (timeGetTime - glTimePuckScored > glDefaultDelayTime) And gfScored And Not gfGameOver And ((timeGetTime - glTimePuckScored < glDefaultDelayTimeGone + glDefaultDelayTime)) Then
  79.             goPuck.DefaultStartPosition
  80.             goPuck.Spinning = True
  81.             goTextLittle.DrawText "Press <Space> to launch puck...", (glScreenWidth / 2) - 50, 25, &HFF0000FF
  82.         End If
  83.         
  84.         'Here is a little helper text letting the user know to press F1
  85.         'to turn of the room (will show up after 3 seconds, and stay on for 10 seconds)
  86.         If (gfDrawRoomText And goRoom.DrawRoom) And (timeGetTime - glTimeNoRoom < glDefaultDelayTimeGone) Then
  87.             goPuck.DefaultStartPosition
  88.             goPuck.Spinning = True
  89.             goTextLittle.DrawText "You can press F1 to turn off the drawing " & vbCrLf & " of the room, which will increase performance.", -15, glScreenHeight - 50, &HFFFF00FF
  90.         End If
  91.         If gfGameOver And ((timeGetTime - glTimeGameOver) > glDefaultDelayTime) And ((timeGetTime - glTimeGameOver < glDefaultDelayTimeGone + glDefaultDelayTime)) Then
  92.             goTextLittle.DrawText "Press F3 to restart...", (glScreenWidth / 2) - 50, 25, &HFF0000FF
  93.         End If
  94.         goTextLittle.EndText
  95.         goTextBig.BeginText
  96.         If gfGameOver Then
  97.             If gfMultiplayer Then
  98.                 If glPlayerScore(glMyPaddleID) > glPlayerScore(Abs(glMyPaddleID - 1)) Then
  99.                     goTextBig.DrawText "Game over!!" & vbCrLf & "You win!!", (glScreenWidth / 2) - (glScreenWidth / 4), (glScreenHeight / 2) - (glScreenHeight / 4), &HFFDD11AA
  100.                 Else
  101.                     goTextBig.DrawText "Game over!!" & vbCrLf & "You lose!!", (glScreenWidth / 2) - (glScreenWidth / 4), (glScreenHeight / 2) - (glScreenHeight / 4), &HFFDD11AA
  102.                 End If
  103.             Else
  104.                 If glPlayerScore(0) > glPlayerScore(1) Then
  105.                     goTextBig.DrawText "Game over!!" & vbCrLf & "You win!!", (glScreenWidth / 2) - (glScreenWidth / 4), (glScreenHeight / 2) - (glScreenHeight / 4), &HFFDD11AA
  106.                 Else
  107.                     goTextBig.DrawText "Game over!!" & vbCrLf & "You lose!!", (glScreenWidth / 2) - (glScreenWidth / 4), (glScreenHeight / 2) - (glScreenHeight / 4), &HFFDD11AA
  108.                 End If
  109.             End If
  110.         End If
  111.         If gfMultiplayer And gfNoSendData Then 'We've been disconnected sometime, notify the user
  112.             goTextBig.DrawText "The connection with the other" & vbCrLf & "system was lost.", 5, (glScreenHeight / 2) - (glScreenHeight / 4), &HFFFFFF00
  113.         End If
  114.         goTextBig.EndText
  115.     End If
  116.     ' End the scene
  117.     g_dev.EndScene
  118.     ' Present the backbuffer contents to the front buffer (screen)
  119.     D3DUtil_PresentAll 0
  120.     
  121. End Sub
  122.  
  123. Public Sub SetupMatrices()
  124.     
  125.     Dim matView As D3DMATRIX
  126.         
  127.     D3DXMatrixLookAtLH matView, goCamera.Position, vec3(0#, 0#, 0#), vec3(0#, 1#, 0#)
  128.     g_dev.SetTransform D3DTS_VIEW, matView
  129.     
  130.     Dim matProj As D3DMATRIX
  131.     D3DXMatrixPerspectiveFovLH matProj, g_pi / 4, 1, 1, 110
  132.     g_dev.SetTransform D3DTS_PROJECTION, matProj
  133.  
  134. End Sub
  135.  
  136. Public Sub RestoreDeviceObjects()
  137.  
  138.     ' Set miscellaneous render states
  139.     With g_dev
  140.         ' Set world transform
  141.         Dim matWorld As D3DMATRIX
  142.         D3DXMatrixIdentity matWorld
  143.         .SetTransform D3DTS_WORLD, matWorld
  144.         ' Set the projection matrix
  145.         Dim matProj As D3DMATRIX
  146.         Dim fAspect As Single
  147.         fAspect = 1
  148.         D3DXMatrixPerspectiveFovLH matProj, g_pi / 3, fAspect, 1, 1000
  149.         .SetTransform D3DTS_PROJECTION, matProj
  150.         
  151.         .SetTextureStageState 0, D3DTSS_COLORARG1, D3DTA_TEXTURE
  152.         .SetTextureStageState 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE
  153.         .SetTextureStageState 0, D3DTSS_COLOROP, D3DTOP_MODULATE
  154.         .SetTextureStageState 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR
  155.         .SetTextureStageState 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR
  156.         .SetTextureStageState 1, D3DTSS_MINFILTER, D3DTEXF_LINEAR
  157.         .SetTextureStageState 1, D3DTSS_MAGFILTER, D3DTEXF_LINEAR
  158.  
  159.         ' Set default render states
  160.         .SetRenderState D3DRS_DITHERENABLE, 1 'True
  161.         .SetRenderState D3DRS_SPECULARENABLE, 0 'False
  162.         .SetRenderState D3DRS_ZENABLE, 1 'True
  163.         .SetRenderState D3DRS_NORMALIZENORMALS, 1 'True
  164.  
  165.     End With
  166.     
  167.     Set goTextBig = Nothing
  168.     Set goTextLittle = Nothing
  169.     
  170.     'Now create a new text object
  171.     Set goTextLittle = New cText
  172.     goTextLittle.InitText g_d3dx, g_dev, "Times New Roman", 8, True
  173.     Set goTextBig = New cText
  174.     goTextBig.InitText g_d3dx, g_dev, "Times New Roman", 18, True
  175.     
  176. End Sub
  177.  
  178. Public Sub InitDefaultLights(Optional ByVal lNumLights As Long = 2, Optional ByVal fFullAmbiantOnly As Boolean = False)
  179.     With g_dev
  180.         ' Set ambient light
  181.         .SetRenderState D3DRS_AMBIENT, &HFFFFFFFF
  182.         goFade.AmbientColor = &HFFFFFFFF
  183.         goFade.MaxAmbientColor = goFade.AmbientColor
  184.         If fFullAmbiantOnly Then Exit Sub
  185.         If lNumLights < 1 Then Exit Sub 'Nothing to do
  186.         
  187.         ' Set ambient light
  188.         'We will slowly lower the ambient light as each new light gets added
  189.         .SetRenderState D3DRS_AMBIENT, &HFFBBBBBB
  190.         goFade.AmbientColor = &HFFBBBBBB
  191.         ' Turn on lighting
  192.         .SetRenderState D3DRS_LIGHTING, 1
  193.         
  194.         'Turn on two lights one on each end of the table
  195.         Dim light As D3DLIGHT8
  196.         
  197.         If lNumLights > 0 Then
  198.             With light
  199.                 .Type = D3DLIGHT_DIRECTIONAL
  200.                 .diffuse.r = 0
  201.                 .diffuse.g = 0
  202.                 .diffuse.b = 0
  203.                 .Direction.x = 0
  204.                 .Direction.y = -10
  205.                 .Direction.z = 0
  206.                 .Range = 100000#
  207.                 .Position.x = 0
  208.                 .Position.y = 3
  209.                 .Position.z = 0
  210.             End With
  211.             goFade.LightColor(0) = light
  212.     
  213.             .SetLight 0, light                   'let d3d know about the light
  214.             .LightEnable 0, 1                    'turn it on
  215.             goFade.NumLight = 0
  216.         End If
  217.         
  218.         
  219.         If lNumLights > 1 Then
  220.             .SetRenderState D3DRS_AMBIENT, &HFFAAAAAA
  221.             goFade.AmbientColor = &HFFAAAAAA
  222.             'Now turn on the second light if we can
  223.             With light
  224.                 .Type = D3DLIGHT_DIRECTIONAL
  225.                 .Direction.x = 15
  226.                 .Direction.y = -10
  227.                 .Direction.z = -15
  228.                 .Range = 1000#
  229.                 .Position.x = -15
  230.                 .Position.y = 10
  231.                 .Position.z = 15
  232.             End With
  233.             goFade.LightColor(1) = light
  234.     
  235.             .SetLight 1, light                   'let d3d know about the light
  236.             .LightEnable 1, 1                    'turn it on
  237.             goFade.NumLight = 1
  238.         End If
  239.  
  240.         
  241.         If lNumLights > 3 Then
  242.             .SetRenderState D3DRS_AMBIENT, 0
  243.             goFade.AmbientColor = 0
  244.             'Now turn on the third light if we can
  245.             With light
  246.                 .Type = D3DLIGHT_DIRECTIONAL
  247.                 .Direction.x = -15
  248.                 .Direction.y = 10
  249.                 .Direction.z = 15
  250.                 .Range = 1000#
  251.                 .Position.x = 15
  252.                 .Position.y = -10
  253.                 .Position.z = -15
  254.             End With
  255.             goFade.LightColor(2) = light
  256.     
  257.             .SetLight 2, light                   'let d3d know about the light
  258.             .LightEnable 2, 1                    'turn it on
  259.             goFade.NumLight = 2
  260.         End If
  261.     End With
  262.     goFade.MaxAmbientColor = goFade.AmbientColor
  263.  
  264. End Sub
  265.